const { ethers } = require('ethers');

async function signDataWithPrivateKey(privateKey) {
    const provider = new ethers.providers.JsonRpcProvider('https://aia-dataseed1-testnet.aiachain.org');
    const wallet = new ethers.Wallet(privateKey, provider);
    const rewardType = [1, 2];
    const rewardId = [1, 2];
    const rewardAmount = [1, 2];
    const isTax = true;
    const id = 1;


    //这里 是前端的调用者 
    const msgSender = '0xd3c0b6Aa1538d639912789be705F18b5Fd89fcE6';

    const hash1 = ethers.utils.keccak256(ethers.utils.solidityPack(['uint[]', 'uint[]'], [rewardType, rewardId]));
    const hash2 = ethers.utils.keccak256(ethers.utils.solidityPack(['uint[]'], [rewardAmount]));
    const hash3 = ethers.utils.keccak256(ethers.utils.solidityPack(['uint', 'address'], [id, msgSender]));
    const hash4 = ethers.utils.keccak256(ethers.utils.solidityPack(['bool'], [isTax]));
    const finalHash = ethers.utils.keccak256(ethers.utils.solidityPack(['bytes32', 'bytes32', 'bytes32', 'bytes32'], [hash1, hash2, hash3, hash4]));

    console.log('Hash:', finalHash);
    const signature = await wallet.signMessage(ethers.utils.arrayify(finalHash));
    console.log('signature',signature);
    const { r, s, v } = ethers.utils.splitSignature(signature);

    return { r, s, v };
}

const privateKey = "e714d873c046aa25df761d7f0f263caa2d4594778c8174e075bfeb26a9b968f1";
signDataWithPrivateKey(privateKey).then(result => {
    console.log("r:", result.r);
    console.log("s:", result.s);
    console.log("v:", result.v);
});